
PlayerMove = {};
local this = PlayerMove;

local transform;
local gameObject;
local lateTime=0;
local firePos;
local flySpeed = 20;
local maxX;
local maxY;
local PlayerHp=5;


function PlayerMove.New()
    local screenMax = Camera.main:ScreenToWorldPoint(Vector3(Screen.width, Screen.height));
    maxX = screenMax.x;
    maxY = screenMax.y;
    gameObject=newObject(ThunderManager.prefabs[1]);
    transform=gameObject.transform;
    transform.position=Vector3(0,0,-9);
    firePos=transform:Find("firePos");
    local luaTrigger = gameObject:AddComponent(typeof(LuaRigidTrigger));
    luaTrigger:AddHandler(this.OnTriggerEnter2D);
    this.HPText=GameObject.Find("GamePanel/HP"):GetComponent(typeof(Text));
    UpdateBeat:Add(this.Update);
end

function PlayerMove.Update()
        this.Move();
        this.ChickFire();
        this.HPText.text="HP:"..PlayerHp;
end

function PlayerMove.Move()
    if (Input.GetMouseButton(0))then
        local pos = Camera.main:ScreenToWorldPoint(Input.mousePosition);
        pos.z=-9;
        pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
        pos.y = Mathf.Clamp(pos.y, -maxY, maxY);

        local vec = pos - transform.position;
        if (Vector3.SqrMagnitude(vec) < 0.5)then
            transform.position = pos;
        else
            local dir = Vector3.Normalize(vec);
            transform:Translate(dir * flySpeed * Time.deltaTime);
        end
    end
end

function PlayerMove.ChickFire()
    lateTime=lateTime+Time.deltaTime;
    if(lateTime>0.3)then
        lateTime=0;
        
        BulletMove.new(firePos,10);
    end
    
end
function PlayerMove.Hurt()
    PlayerHp=PlayerHp-1;
    if(PlayerHp==0)then
    UpdateBeat:Remove(PlayerMove.Update)
    destroy(gameObject);
    this.HPText.text="HP:0";
    end
end

function PlayerMove.OnTriggerEnter2D(collider)
   
end